-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Per module lm history #8199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Per module lm history #8199
Conversation
@@ -17,10 +17,10 @@ | |||
|
|||
class Predict(Module, Parameter): | |||
def __init__(self, signature, callbacks=None, **config): | |||
super().__init__(callbacks=callbacks) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw typical custom DSPy programs don't call super().__init__(...)
.
Will that be an issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing will crash, but they will lose some perks from dspy.Module
. We can follow what PyTorch does - recommending calling super().__init__()
when customizing modules: https://docs.pytorch.org/tutorials/beginner/examples_nn/polynomial_module.html#pytorch-custom-nn-modules
@@ -20,26 +21,38 @@ def _base_init(self): | |||
def __init__(self, callbacks=None): | |||
self.callbacks = callbacks or [] | |||
self._compiled = False | |||
# LM calling history of the module. | |||
self.history = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! Does this work if there are multiple LMs used in the module for different sub-modules?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes! This per-module history works the same for multi-lm situation.
Just tested with the following code and no issue:
import dspy
class MyProgram(dspy.Module):
def __init__(self):
super().__init__()
self.cot = dspy.ChainOfThought("question -> answer")
self.cot2 = dspy.ChainOfThought("question, answer -> judgement")
self.cot2.predict.lm = dspy.LM("openai/gpt-4o", cache=False)
def forward(self, question: str, **kwargs) -> str:
answer = self.cot(question=question).answer
return self.cot2(question=question, answer=answer)
dspy.settings.configure(lm=dspy.LM("openai/gpt-4o-mini", cache=False), adapter=dspy.JSONAdapter())
program = MyProgram()
program(question="What is the capital of France?")
print(program.history)
Make it possible to do
module.inspect_history
, which gives the LM history of the module. In nesting case, the same LM history message will be available to all the ancestor callers.Example code:
It will give:
You can also access the history by
program.cot.inspect_history
.